package com.example.exercise.feedback.service;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.ArrayList;
import java.util.List;

import com.example.exercise.feedback.vo.AppFeedbackVO;
import com.example.exercise.feedback.dao.entity.AppFeedbackEntity;
import com.example.exercise.feedback.dao.AppFeedbackDAO;

/**
* 
*/
@Service
public class AppFeedbackService {

    @Autowired
    private AppFeedbackDAO appFeedbackDAO;

    public List<AppFeedbackVO> findByWhere(Page<?> page, AppFeedbackVO appFeedbackVO){
        List<AppFeedbackVO> list = new ArrayList<>();
        List<AppFeedbackEntity> list0 = appFeedbackDAO.findByWhere(page, appFeedbackVO);
        for (AppFeedbackEntity appFeedbackEntity : list0) {
            list.add(toAppFeedbackVO(appFeedbackEntity));
        }
        return list;
    }

    public AppFeedbackVO findByKey(AppFeedbackVO appFeedbackVO){
        AppFeedbackEntity appFeedbackEntity = appFeedbackDAO.findByKey(appFeedbackVO);
        return toAppFeedbackVO(appFeedbackEntity);
    }

    public int insert(AppFeedbackVO appFeedbackVO){
        return appFeedbackDAO.insert(toAppFeedbackEntity(appFeedbackVO));
    }

    public int updateByKey(AppFeedbackVO appFeedbackVO){
        return appFeedbackDAO.updateByKey(toAppFeedbackEntity(appFeedbackVO));
    }

    public int deleteByKey(AppFeedbackVO appFeedbackVO){
        return appFeedbackDAO.deleteByKey(appFeedbackVO);
    }

    private AppFeedbackVO toAppFeedbackVO(AppFeedbackEntity appFeedbackEntity) {
        AppFeedbackVO appFeedbackVO = new AppFeedbackVO();
                appFeedbackVO.setId(String.valueOf(appFeedbackEntity.getId()));
        appFeedbackVO.setUserId(String.valueOf(appFeedbackEntity.getUserId()));
        appFeedbackVO.setUserName(appFeedbackEntity.getUserName());
        appFeedbackVO.setMobile(appFeedbackEntity.getMobile());
        appFeedbackVO.setAppVersion(appFeedbackEntity.getAppVersion());
        appFeedbackVO.setContent(appFeedbackEntity.getContent());
        appFeedbackVO.setOnlineState(appFeedbackEntity.getOnlineState());
        appFeedbackVO.setOsType(appFeedbackEntity.getOsType());
        appFeedbackVO.setSysVersion(appFeedbackEntity.getSysVersion());
        appFeedbackVO.setReplyCount(String.valueOf(appFeedbackEntity.getReplyCount()));

        return appFeedbackVO;
    }

    private AppFeedbackEntity toAppFeedbackEntity(AppFeedbackVO appFeedbackVO) {
        AppFeedbackEntity appFeedbackEntity = new AppFeedbackEntity();
                String id = appFeedbackVO.getId();
        if(id != null) {
            appFeedbackEntity.setId(Integer.parseInt(appFeedbackVO.getId()));
        }
        String userId = appFeedbackVO.getUserId();
        if(userId != null) {
            appFeedbackEntity.setUserId(Integer.parseInt(appFeedbackVO.getUserId()));
        }
        appFeedbackEntity.setUserName(appFeedbackVO.getUserName());
        appFeedbackEntity.setMobile(appFeedbackVO.getMobile());
        appFeedbackEntity.setAppVersion(appFeedbackVO.getAppVersion());
        appFeedbackEntity.setContent(appFeedbackVO.getContent());
        appFeedbackEntity.setOnlineState(appFeedbackVO.getOnlineState());
        appFeedbackEntity.setOsType(appFeedbackVO.getOsType());
        appFeedbackEntity.setSysVersion(appFeedbackVO.getSysVersion());
        String replyCount = appFeedbackVO.getReplyCount();
        if(replyCount != null) {
            appFeedbackEntity.setReplyCount(Integer.parseInt(appFeedbackVO.getReplyCount()));
        }

        return appFeedbackEntity;
    }

}